package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.Station;
import com.wcar.common.entity.Store;
import com.wcar.common.entity.User;
import com.wcar.common.entity.UserRole;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.StantionDao;
import com.wcar.store.dao.StoreDao;
import com.wcar.store.dao.UserRoleDao;
import com.wcar.store.service.StationService;
import com.wcar.store.vo.StationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-05-24  14:43
 */
@Service
public class StationServiceImpl implements StationService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private StantionDao stantionDao;
    @Resource
    private StoreDao storeDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Override
    public Result<IPage> selectStatinAll(StationVo stationVo) {
        IPage<Station> iPage=new Page<>(stationVo.getCurrentPage(),stationVo.getPageSize());
        QueryWrapper<Station> wrapper=new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        }
        //判断是不是总店长
        if(!roleIds.contains(1)){
            wrapper.eq("tsu.user_id",user.getUserId());
        }
        if(stationVo.getStoreName()!=null&&StringUtils.hasText(stationVo.getStoreName())){
            wrapper.like("ts.store_name",stationVo.getStoreName());
        }
        IPage<Station> pageInfo=stantionDao.selectStationAll(iPage,wrapper);
        return new Result<IPage>(200,"获取门店工位成功",pageInfo);
    }

    @Override
    public Result<List<Store>> selectStoreAll() {
        QueryWrapper<Store> wrapper=new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        }
        //判断是不是总店长
        if(!roleIds.contains(1)){
            wrapper.eq("tu.user_id",user.getUserId());
        }
        List<Store> storeList=storeDao.selectStore(wrapper);
        return new Result<List<Store>>(200,"获取所有门店信息成功",storeList);
    }

    @Override
    public Result<String> insertStan(Station station) {
        int insert = stantionDao.insert(station);
        return new Result<String>(200,"添加成功",insert+"");
    }

    @Override
    public Result<String> updateSta(Station station) {
        UpdateWrapper<Station> wrapper=new UpdateWrapper<>();
        wrapper.eq("station",station.getStation());
        int update = stantionDao.update(station, wrapper);
        return new Result<String>(200,"修改成功",update+"");
    }

    @Override
    public Result<String> deleteSta(Integer station) {
        int i = stantionDao.deleteById(station);
        return new Result<String>(200,"删除成功",i+"");
    }

    @Override
    public Result<List<Station>> selectByStoreId(Integer storeId) {
        QueryWrapper<Station> wrapper=new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        List<Station> stations = stantionDao.selectList(wrapper);
        return new Result<>(200,"根据Idh获取门店信息成功",stations);
    }
}
